package com.our.sys.project1.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.sys.project1.dao.ValuationProDao;
import com.our.sys.project1.entity.ValuationPro;

@Service
@Transactional
public class ValuationProService {
    
    private ValuationProDao valuationProDao;

    private static Logger logger = LoggerFactory.getLogger(UsedTypeService.class);

    @Transactional(readOnly = true)
    public Page<ValuationPro> query(Page<ValuationPro> page, List<PropertyFilter> filters) {
        Page<ValuationPro> curPage = valuationProDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public ValuationPro queryById(Long id) {
        return valuationProDao.findOneBy("id", id);
    }
    
    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = valuationProDao.findUnique("select count(a) from ValuationPro a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }
    
    @Transactional(readOnly = true)
    private Long queryCountByCode(String code) {
        Long result = valuationProDao.findUnique("select count(a) from ValuationPro a where a.code = ?", code);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }
    
    public void save(ValuationPro valuationPro) throws Exception {
        try {
            if (null != valuationPro.getId()) {
                ValuationPro getValuationPro = this.queryById(valuationPro.getId());

                if (queryCountByName(getValuationPro.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个产品属性名称！");
                }
                
                if (queryCountByCode(getValuationPro.getCode()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个产品属性代码！");
                }

                getValuationPro.setCode(valuationPro.getCode());
                getValuationPro.setName(valuationPro.getName());
                valuationProDao.save(getValuationPro);
            } else {

                if (queryCountByName(valuationPro.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个产品属性名称！");
                }
                
                if (queryCountByCode(valuationPro.getCode()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个产品属性代码！");
                }
                valuationProDao.save(valuationPro);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            valuationProDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
